package cn.yunhe.crm.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.yunhe.crm.dao.DeptMapper;
import cn.yunhe.crm.dao.EmpMapper;
import cn.yunhe.crm.dao.OrdersMapper;
import cn.yunhe.crm.entity.Dept;
import cn.yunhe.crm.entity.Emp;
import cn.yunhe.crm.entity.Orders;
import cn.yunhe.crm.entity.Tasklist;
import cn.yunhe.crm.service.TasklistService;

@Scope("prototype")
@Controller
public class TasklistController {

	@Autowired
	private TasklistService taskService;
	
	public void setTaskService(TasklistService taskService) {
		this.taskService = taskService;
	}

	@Autowired
	private DeptMapper dMapper;

	public void setdMapper(DeptMapper dMapper) {
		this.dMapper = dMapper;
	}
	
	@Autowired
	private EmpMapper eMapper;
	
	public void seteMapper(EmpMapper eMapper) {
		this.eMapper = eMapper;
	}
	
	@Autowired
	private OrdersMapper ordersMapper;
	
	
	public void setOrders(OrdersMapper ordersMapper) {
		this.ordersMapper = ordersMapper;
	}

	// ��ѯ��������
	@RequestMapping(value = "taskmangae.do")
	public String toindex(HttpSession session) {
		List<Tasklist> tasklist = taskService.findAllTask();
		session.removeAttribute("tasklist");
		session.setAttribute("tasklist", tasklist);
		return "taskmangae";
	}

	// �鿴��������
	@RequestMapping(value = "task_detail.do")
	public String getTask_detail(HttpSession session,Integer id) {
		Tasklist task = taskService.findTaskById(id);
		session.removeAttribute("task");
		session.setAttribute("task", task);
		return "task_detail";
	}
	
	// ��ת�����ҳ��
	@RequestMapping(value = "task_add.do")
	public String toAdd(HttpSession session){
		List<Dept> depts = dMapper.selectByExample(null);
		List<Emp> emps = eMapper.selectByExample(null);
		List<Orders> orders = ordersMapper.selectByExample(null);
		session.removeAttribute("depts");
		session.setAttribute("depts", depts);
		session.removeAttribute("emps");
		session.setAttribute("emps", emps);
		session.removeAttribute("orders");
		session.setAttribute("orders", orders);
		return "task_add";
	}
	
	//��Ӳ��ض���
	@RequestMapping(value = "addtask.do",method={RequestMethod.POST})
	public String addTask(Tasklist tasklist){
		taskService.saveTask(tasklist);
		return "redirect:/taskmangae.do";
	}
}
